﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using HML.Design;

namespace HML
{
    /// <summary>
    /// 面包屑导航选项集合
    /// </summary>
    [Editor(typeof(CollectionTextHelpEditor), typeof(UITypeEditor))]
    public class BreadcrumbBarItemCollection : IList, ICollection, IEnumerable
    {
        #region

        private ArrayList navigationBarItemList = new ArrayList();

        private BreadcrumbBar owner;

        #endregion

        internal BreadcrumbBarItemCollection(BreadcrumbBar owner)
        {
            this.owner = owner;
        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            BreadcrumbBarItem[] listArray = new BreadcrumbBarItem[this.navigationBarItemList.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (BreadcrumbBarItem)this.navigationBarItemList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.navigationBarItemList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.navigationBarItemList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is BreadcrumbBarItem))
            {
                throw new ArgumentException("BreadcrumbBarItem");
            }
            return this.Add((BreadcrumbBarItem)value);
        }

        public int Add(BreadcrumbBarItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (item.Owner != null)
            {
                throw new ArgumentException("BreadcrumbBarItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            item.Owner = this.owner;
            this.navigationBarItemList.Add(item);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
            return this.Count - 1;
        }

        public void Clear()
        {
            for (int i = 0; i < this.navigationBarItemList.Count; i++)
            {
                ((BreadcrumbBarItem)this.navigationBarItemList[i]).Owner = null;
            }
            this.navigationBarItemList.Clear();
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
                this.owner.SelectedItem = this.owner.SelectedItem;
            }
        }

        public bool Contains(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object item)
        {
            if (item is BreadcrumbBarItem)
            {
                return this.Contains((BreadcrumbBarItem)item);
            }
            return false;
        }

        public int IndexOf(object item)
        {
            if (item is BreadcrumbBarItem)
            {
                return this.navigationBarItemList.IndexOf(item);
            }
            return -1;
        }

        public BreadcrumbBarItem Find(int index)
        {
            if (index < 0 || index >= this.Count)
            {
                return null;
            }
            return (BreadcrumbBarItem)this.navigationBarItemList[index];
        }

        public void Insert(int index, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is BreadcrumbBarItem))
            {
                throw new ArgumentException("类型应该为 BreadcrumbBarItem");
            }
            BreadcrumbBarItem item = (BreadcrumbBarItem)value;
            if (item.Owner != null)
            {
                throw new ArgumentException("BreadcrumbBarItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            if (index > this.Count)
            {
                index = this.Count;
            }
            ArrayList tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                tmp.Add(this.navigationBarItemList[i]);
            }
            item.Owner = this.owner;
            tmp.Add(value);
            for (int i = index; i < this.Count; i++)
            {
                tmp.Add(this.navigationBarItemList[i]);
            }
            this.navigationBarItemList = tmp;
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is BreadcrumbBarItem))
            {
                throw new ArgumentException("BreadcrumbBarItem");
            }
            this.Remove((BreadcrumbBarItem)value);
        }

        public void Remove(BreadcrumbBarItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            item.Owner = null;
            this.navigationBarItemList.Remove(item);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
                this.owner.SelectedItem = this.owner.SelectedItem;
            }
        }

        public void RemoveAt(int index)
        {
            ((BreadcrumbBarItem)this.navigationBarItemList[index]).Owner = null;
            this.navigationBarItemList.RemoveAt(index);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
                this.owner.SelectedItem = this.owner.SelectedItem;
            }
        }

        public BreadcrumbBarItem this[int index]
        {
            get { return (BreadcrumbBarItem)this.navigationBarItemList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (value.Owner != null)
                {
                    throw new ArgumentException("BreadcrumbBarItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
                }

                ((BreadcrumbBarItem)this.navigationBarItemList[index]).Owner = null;
                value.Owner = this.owner;
                this.navigationBarItemList[index] = value;
                if (this.owner != null)
                {
                    this.owner.InitializeElement();
                    this.owner.Invalidate();
                    this.owner.SelectedItem = this.owner.SelectedItem;
                }
            }
        }

        object IList.this[int index]
        {
            get { return (object)this.navigationBarItemList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (!(value is BreadcrumbBarItem))
                {
                    throw new ArgumentException("类型应该为 BreadcrumbBarItem");
                }
                if (((BreadcrumbBarItem)value).Owner != null)
                {
                    throw new ArgumentException("BreadcrumbBarItem选项不应该同时被添加到多个地方，请先从原有地方移除再添加");
                }

              ((BreadcrumbBarItem)this.navigationBarItemList[index]).Owner = null;
                ((BreadcrumbBarItem)value).Owner = this.owner;
                this.navigationBarItemList[index] = value;
                if (this.owner != null)
                {
                    this.owner.InitializeElement();
                    this.owner.Invalidate();
                    this.owner.SelectedItem = this.owner.SelectedItem;
                }
            }
        }

        #endregion

    }


}
